package backupit.gui.tree;

import backupit.core.FileInfo;
import backupit.core.FileInfoSet;
import java.io.File;
import java.util.StringTokenizer;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author dbotelho
 */
public class FileInfoSetTreeNode extends DefaultMutableTreeNode {

    public FileInfoSetTreeNode(FileInfoSet set) {
        FileInfoSetTreeNode curr, n = null;
        for (FileInfo file : set) {

            curr = this;
            StringTokenizer tok = new StringTokenizer(file.getRelativePath(), File.separator);
            while (tok.hasMoreTokens()) {
                String child_name = tok.nextToken();
                n = curr.findChild(child_name);
                if (n == null) {
                    
                    n=new FileInfoSetTreeNode(child_name);
                    curr.add(n);
                }
                curr = n;
            }
        }
    }

    private FileInfoSetTreeNode(String child_name) {
        super(child_name);
    }
/*
    public static void main(String[] args){
        JFrame frame = new JFrame();
        FileInfoSetReader source = new FileInfoSetReader("/home/dbotelho/Desktop/FEUP/aaco/trab", true, true, true);
        FileInfoSet set = source.loadFiles();

        FileInfoSetTreeNode node = new FileInfoSetTreeNode(set);
        //tree.findDirectory("J");
        JTree tree = new JTree(node);
        JScrollPane pane = new JScrollPane(tree);
        frame.getContentPane().add(pane);
        frame.pack();
        frame.setVisible(true);
    }*/

    private FileInfoSetTreeNode findChild(String child_name) {
        FileInfoSetTreeNode child;
        if(child_name == null || getChildCount()==0)
            return null;
        for(int i=0; i < getChildCount();i++){
            child = (FileInfoSetTreeNode)getChildAt(i);
            if(child_name==null);
            if(child.toString().equals(child_name)) return child;
        }
        return null;
    }
}
